﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using FlickrXplorer.Web.ViewData;
using FlickrXplorer.Web.Utilities;
using log4net;

namespace FlickrXplorer.Web
{
    public class GlobalApplication : HttpApplication
    {
        private readonly bool IngegratedMode = Convert.ToBoolean(ConfigurationManager.AppSettings["IntegratedMode"], System.Globalization.CultureInfo.InvariantCulture);
        private static readonly string BookmarkPath = ConfigurationManager.AppSettings["bookmark_path"];
        private static readonly BookmarkCollection bookmarks = HtmlExtensions.LoadBookmark(AppDomain.CurrentDomain.BaseDirectory + BookmarkPath);
        private static readonly ILog log = LogManager.GetLogger("FlickrXplorer.Logging");

        public void RegisterRoutes(RouteCollection routes)
        {
            if (IngegratedMode)
            {

                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapRoute("Tags", "Photo/Tags/{name}", new { controller = "Photo", action = "Tags" });
                routes.MapRoute("Detail", "Photo/Detail", new {controller = "Photo", action = "Detail"});
                routes.MapRoute("PopularTags", "Photo/PopularTags", new { controller = "Photo", action = "PopularTags" });
                routes.MapRoute("User", "Photo/User/{u}", new {controller = "Photo", action = "User"});
                routes.MapRoute("NsId", "Photo/NsId/{nsId}", new {controller = "Photo", action = "NsId"});
                routes.MapRoute("Search", "Photo/Search", new {controller = "Photo", action = "Search"});
                routes.MapRoute("Comments", "Comment/List", new {controller = "Comment", action = "List"});
                routes.MapRoute("Reload", "Security/Auth", new { controller = "Security", action = "Auth" });
                routes.MapRoute("Logout", "Security/Logout", new { controller = "Security", action = "Logout" });
                routes.MapRoute("Debug", "Debug", new { controller = "Debug", action = "Index" });
                routes.MapRoute("Category", "Photo/{category}", new { controller = "Photo", action = "Index" });
                routes.MapRoute("Index", "{controller}/{action}");
              
            }
            else
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute("Tags", "Photo.mvc/Tags/{name}", new { controller = "Photo", action = "Tags" });
                routes.MapRoute("Detail", "Photo.mvc/Detail", new { controller = "Photo", action = "Detail" });
                routes.MapRoute("PopularTags", "Photo.mvc/PopularTags", new { controller = "Photo", action = "PopularTags" });
                routes.MapRoute("User", "Photo.mvc/User/{u}", new { controller = "Photo", action = "User" });
                routes.MapRoute("NsId", "Photo.mvc/NsId/{nsId}", new { controller = "Photo", action = "NsId" });
                routes.MapRoute("Search", "Photo.mvc/Search", new { controller = "Photo", action = "Search" });
                routes.MapRoute("Comments", "Comment.mvc/List", new { controller = "Comment", action = "List" });
                routes.MapRoute("Reload", "Security.mvc/Auth", new { controller = "Security", action = "Auth" });
                routes.MapRoute("Logout", "Security.mvc/Logout", new { controller = "Security", action = "Logout" });
                routes.MapRoute("Debug", "Debug.mvc", new { controller = "Debug", action = "Index" });
                routes.MapRoute("Category", "Photo.mvc/{category}", new { controller = "Photo", action = "Index" });
           
                routes.MapRoute("Index", "{controller}.mvc/{action}");

            }

            //ViewEngines.Engines.Add(new NHamlViewEngine());
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            Context.Items.Add("IntegratedMode", IngegratedMode);
            Context.Items.Add("Bookmark", bookmarks);
            Context.Items.Add("Logger", log);
        }
    }
}
